apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    app: {{ kubernetes.app_name }}
    type: {{ kubernetes.labels.type }}
  name: {{ kubernetes.deployment_name }}
  namespace: {{ kubernetes.namespace }}
spec:
  replicas: {{ kubernetes.replicas }}
  selector:
    matchLabels:
      app: {{ kubernetes.app_name }}
      type: {{ kubernetes.labels.type }}
  strategy:
    type: RollingUpdate
    rollingUpdate:
      {% if kubernetes.rollouts and kubernetes.rollouts.max_surge %}
      maxSurge: {{ kubernetes.rollouts.max_surge }}
      {% endif %}
      {% if kubernetes.rollouts and kubernetes.rollouts.max_unavailable %}
      maxUnavailable: {{ kubernetes.rollouts.max_unavailable }}
      {% endif %}
  template:
    metadata:
      labels:
        app: {{ kubernetes.app_name }}
        type: {{ kubernetes.labels.type }}
      name: {{ kubernetes.deployment_name }}
      namespace: {{ kubernetes.namespace }}
    spec:
      containers:
      - args:
        - {{ kubernetes.command }}
        command:
        - /bin/bash
        - -c
        env:
            {%- for name in app|sort -%}
              {% set value = app[name] %}
            - name: {{ name|upper }}
              {% if value == 'SECRET' -%}
              valueFrom:
                secretKeyRef:
                  key: {{ name|replace('_', '-') }}
                  name: "{{ kubernetes.secrets_name }}"
              {%- else -%}
                value: "{{ value }}"
              {%- endif -%}
            {%- endfor %}
        image: {{ kubernetes.image.repo }}:{{ kubernetes.image.tag }}
        imagePullPolicy: {{ kubernetes.image.pull_policy }}
        name: sumo-{{ kubernetes.target_environment }}-{{ kubernetes.labels.type }}
        {%- if kubernetes.readiness %}
        readinessProbe:
          httpGet:
              port: {{ app.port }}
              path: /readiness/
              httpHeaders:
                  - name: Host
                    value: {{ app.allowed_hosts.split(',')[0] }}
          failureThreshold: {{ kubernetes.readiness.failure_threshold }}
          initialDelaySeconds: {{ kubernetes.readiness.initial_delay_seconds }}
          periodSeconds: {{ kubernetes.readiness.period_seconds }}
          successThreshold: {{ kubernetes.readiness.success_threshold }}
          timeoutSeconds: {{ kubernetes.readiness.timeout_seconds }}
        {%- endif %}
        {%- if kubernetes.liveness %}
        livenessProbe:
          tcpSocket:
            port: {{ app.port }}
          failureThreshold: {{ kubernetes.liveness.failure_threshold }}
          initialDelaySeconds: {{ kubernetes.liveness.initial_delay_seconds }}
          periodSeconds: {{ kubernetes.liveness.period_seconds }}
          successThreshold: {{ kubernetes.liveness.success_threshold }}
          timeoutSeconds: {{ kubernetes.liveness.timeout_seconds }}
        {%- endif %}
        resources:
          requests:
            cpu: {{ kubernetes.limits.cpu_request }}
            memory: {{ kubernetes.limits.memory_request }}
          limits:
            cpu: {{ kubernetes.limits.cpu_limit }}
            memory: {{ kubernetes.limits.memory_limit }}
      restartPolicy: Always
